#include "BasicResLoader.h"
#include <tchar.h>



namespace utility
{
	std::map<string_t, Gdiplus::Image*> BasicResLoader::m_ImgList;
	CRITICAL_SECTION BasicResLoader::Crcal_Section;
	HMODULE BasicResLoader::m_hmodule = NULL;

	struct ImElement
	{
		UINT			ImID;
		const char_t*	lpszFace;

	};

	BasicResLoader::BasicResLoader(void)
	{
	}

	BasicResLoader::~BasicResLoader(void)
	{
	}


	bool BasicResLoader::LoadImg()
	{
		if( !m_hmodule )
		{
			m_hmodule = LoadLibrary(  _T("skinface.dll") );
		}
		if( m_hmodule == NULL )
		{
			return false;
		}

		InitializeCriticalSection(&Crcal_Section);
		EnterCriticalSection(&Crcal_Section);

		Gdiplus::Image* pIm;
		ImElement ArrImgList[]={
			//{IDR_BTN_CLOSE,					L"IMAGE_BTN_CLOSE"},
			//{IDR_BTN_GAME,					L"IMAGE_BTN_GAME"},
			{1,_T("1")},
		};

		for(int i=0; i<sizeof(ArrImgList)/sizeof(ArrImgList[0]);i++)
		{
			pIm = LoadResImage(ArrImgList[i].ImID);
			if(pIm)
			{
				m_ImgList[ArrImgList[i].lpszFace] = pIm;
			}
		}

		LeaveCriticalSection(&Crcal_Section);

		if( m_hmodule )
		{
			FreeLibrary( m_hmodule );
			m_hmodule = NULL;
		}
		return true;
	}
	Gdiplus::Image* BasicResLoader::GetResImg(/*LPCTSTR*/const char_t* pStr)
	{
		Gdiplus::Image* pImgResult = NULL;
		EnterCriticalSection(&Crcal_Section);
		if(m_ImgList.find(pStr) != m_ImgList.end())
			pImgResult = m_ImgList[pStr];
		LeaveCriticalSection(&Crcal_Section);
		return pImgResult;
	}
	void BasicResLoader::ReleaseImg()
	{
		EnterCriticalSection(&Crcal_Section);
		for(map<string_t, Gdiplus::Image*>::iterator It = m_ImgList.begin(); It != m_ImgList.end(); It++)
		{
			Gdiplus::Image* pImg = It->second;
			if( pImg )
			{
				delete pImg;
				It->second = NULL;
			}
		}
		m_ImgList.clear();
		LeaveCriticalSection(&Crcal_Section);
		DeleteCriticalSection(&Crcal_Section);
	}

	Gdiplus::Image* BasicResLoader::LoadResImage(UINT uImID)
	{
		HRSRC hResource;
		DWORD imageSize;
		Gdiplus::Image* pIm = NULL;
		bool bOk = false;
		hResource = ::FindResource(m_hmodule/*NULL*/, MAKEINTRESOURCE(uImID),_T("Skin"));
		if (hResource == NULL)
			hResource = ::FindResource(m_hmodule/*NULL*/, MAKEINTRESOURCE(uImID), RT_BITMAP);
		if (hResource == NULL)
			return NULL;
		if ((imageSize = ::SizeofResource(m_hmodule/*NULL*/, hResource)) == 0)
			return NULL;
		const void* pResourceData = ::LockResource(::LoadResource(m_hmodule/*NULL*/, hResource));
		if (!pResourceData)
		{
			return NULL;
		}
		HGLOBAL g_Buffer  = ::GlobalAlloc(GMEM_FIXED, imageSize);
		if (g_Buffer)
		{
			void* pBuffer = ::GlobalLock(g_Buffer);
			if (pBuffer)
			{
				CopyMemory(pBuffer, pResourceData, imageSize);
				IStream* pStream = NULL;
				if (::CreateStreamOnHGlobal(g_Buffer, FALSE, &pStream) == S_OK)
				{
					pIm = Gdiplus::Image::FromStream(pStream); 
					pStream->Release();
					if (pIm)
					{ 
						if (pIm->GetLastStatus() == Gdiplus::Ok)
						{
							bOk = true;
						}
					}
				}
				::GlobalUnlock(g_Buffer);
			}
		}
		return pIm;
	}


	HMODULE BasicResLoader::GetHModule()
	{
		if( !m_hmodule )
		{
			m_hmodule = LoadLibrary( _T("skinface.dll") );;
		}
		return m_hmodule;
	}

	void BasicResLoader::FreeHModule()
	{
		if( m_hmodule )
		{
			FreeLibrary( m_hmodule );
			m_hmodule = NULL;
		}
	}

}
